package com.spzx.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.spzx.common.core.domain.R;
import com.spzx.order.api.RemoteOrderService;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.order.api.domain.OrderItem;
import com.spzx.payment.service.AlipayService;
import com.spzx.payment.service.PaymentService;
import com.spzx.rabbit.constant.MqConst;
import com.spzx.rabbit.service.RabbitService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    RemoteOrderService remoteOrderService;

    @Autowired
    PaymentService paymentService;

    @Autowired
    RabbitService rabbitService;

    String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    String appId = "9021000141683990";
    String spzxPrivateKey ="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCLauUx7sH5bYGSVpCMpH+PXigYAaOgN6Olh8x6TXQDjjnILYW/Yo0WeJ+bWxgbrYESnoH/xeZpKp4D1kfiPkQ9sI7WyTbDIZc0bPSPzpUrVKukAb8SznXlwMso1mJ63H3JPgLfFGS3q05vXe85mkczyHk0U5so509lVfBBD2aJadda96EJuSj6w8VYwDNxUhKYbmXA442iAaVW4HzKN4IaonoMizP5BCZgNaXejK1x7aEkH/l4s7ENWwmGo5zrSc/1sOFBIPCv6Jb5qEzmFrASnkret3qG55RusIoI9ESvVQHK14M+sCLtycvccgXV557egq+XEQnKV6+G6+KBlLCLAgMBAAECggEAdvWO+zwuZxX5KVSTom36Keq9az56z4qqJ+PnpbPQ+fexdgzhwpvkpvfluDnSKEuzhp+ZbRT/tVyX185PJ8zZjYnJV5v5DibrTeBqDQrIXWCx1A9pajKghpoqv32uKgdZjQ1MhexwxAd3ta8BILV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charset = "utf-8";
    String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqCHvmV4lF6bio++cvfO+JvLbGL0ADXrnRnJD3rYKDRo30UFp3C8jEpM0WvasnZOhljq53XL1NNbmVfrVw8UP4oz96N+05cLXF78mf1F9hRGJpRtCq9YzlUE7rPp4EIGNZJAtHO2UcV/QWS2qCCNLdkKbTFhgNlCIwU0uvEfbAh+jZNQLXyu6ErjN+305x2hRA0S4nyd/t+6SB3+3wnx/3ya3pUWi1jdH2KcG1Reh4+VDwikdPbrRIWMVooUVZK9pBeZ8Dupk3av11G5254iaGkE0aQgZahW+ZCVE2818KpbrW7Nuuf+vJaXH6jlEtpHWKQTORL66sM1J8uUHy3h97QIDAQAB";
    @SneakyThrows
    @Override
    public String submitAlipay(String orderNo) {
        // 调用支付宝sdk，生成跳转form


        // 1 创建支付宝客户端实例
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,appId,spzxPrivateKey,format,charset,alipayPublicKey,"RSA2");


        // 2 拼接请求参数对象
        AlipayTradeWapPayRequest alipayTradeWapPayRequest = new AlipayTradeWapPayRequest();
        Map<String,Object> map = new HashMap<>();
        map.put("out_trade_no",orderNo);
        R<OrderInfo> orderByOrderNoR = remoteOrderService.getOrderByOrderNo(orderNo,"inner");
        OrderInfo orderInfo = orderByOrderNoR.getData();
        map.put("total_amount",orderInfo.getTotalAmount());
        String title = "";
        for (OrderItem orderItem : orderInfo.getOrderItemList()) {
            title = title + "," + orderItem.getSkuName();
        }
        map.put("subject",title);// 商品标题
        map.put("product_code","QUICK_WAP_WAY");
        alipayTradeWapPayRequest.setBizContent(JSON.toJSONString(map));

        // 设置特殊参数
        alipayTradeWapPayRequest.setReturnUrl("http://192.168.139.129/#/pages/money/paySuccess");// spzx的重定向回朓页面
        alipayTradeWapPayRequest.setNotifyUrl("http://ayevas.natappfree.cc/payment/alipay/callback");// spzx的接口

        // 3 发送请求
        AlipayTradeWapPayResponse alipayTradeWapPayResponse = alipayClient.pageExecute(alipayTradeWapPayRequest);
        String body = alipayTradeWapPayResponse.getBody();

        // 生成支付信息

        return body;
    }

    @Override
    public void callback(Map<String, String> paramsMap) {
        String out_trade_no = paramsMap.get("out_trade_no");
        System.out.println("支付宝被阿里回调业务");
        System.out.println("更新支付状态");
        // 更新支付状态

        // 通知订单，修改订单状态
        rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,out_trade_no);
    }
}
